// websocket 服务器端实现
package main

import (
	"fmt"
	"html/template"
	"log"
	"net/http"

	"code.google.com/p/go.net/websocket"
)

func Echo(ws *websocket.Conn) {
	var err error

	for {
		var reply string
		fmt.Println("Test for websocket-----------1")
		if err = websocket.Message.Receive(ws, &reply); err != nil {
			fmt.Println("Can`t receive")
			break
		}
		fmt.Println("Received back from client:" + reply)

		msg := "Received: " + reply
		fmt.Println("Sending to client:" + msg)

		if err = websocket.Message.Send(ws, msg); err != nil {
			fmt.Println("Can`t send")
			break
		}
	}
}
func client(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("client.html")
	t.Execute(w, nil)
}

func main() {
	http.Handle("/", websocket.Handler(Echo))
	http.HandleFunc("/chat", client)
	fmt.Println("Test for websocket!")
	if err := http.ListenAndServe(":9090", nil); err != nil {
		log.Fatal("ListenAndServe:", err.Error())
	}

}
